package com.chookss.mine.set;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.mine.entity.MineInfoEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.DrawableTextView;
import com.chookss.view.InitDialog;
import com.chookss.view.listener.ShareDialogListener;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lzy.okgo.model.Response;

import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserMoreInfoActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_txt)
    TextView commonRightTxt;
    @BindView(R.id.edSchool)
    EditText edSchool;
    @BindView(R.id.tvEducation)
    DrawableTextView tvEducation;
    @BindView(R.id.edSpecialty)
    EditText edSpecialty;
    @BindView(R.id.edBornAddress)
    EditText edBornAddress;
    @BindView(R.id.edAddress)
    EditText edAddress;
    @BindView(R.id.edJobAddress)
    EditText edJobAddress;
    @BindView(R.id.edPeople)
    EditText edPeople;
    @BindView(R.id.edPhone)
    EditText edPhone;
    private String[] educationArray = null;
    private MineInfoEntity mineInfoEntity = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_info);
        ButterKnife.bind(this);
        NavigationTools.setWaterMark(this, "18");
    }

    @Override
    public void initData() {
        initState();
        educationArray=new String[]{"高中","大专","本科","硕士","博士"};
        commonTitleTxt.setText("详细资料");
        commonRightTxt.setText("保存");
        commonRightTxt.setVisibility(View.VISIBLE);
        selectEmployeeDetail();
    }
    private void selectEmployeeDetail() {
        Map<String, String> map = new HashMap<>();
        map.put("employeeCode", new ShareUtils().getString(this, StaticClass.EMPLOYEECODE, ""));
        map.put("type", "1");
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<MineInfoEntity>>() {
        }.getType(), null, Urls.selectEmployeeDetail, SignUtils2.makeSignStr(map), false, new ObserverCallback<MineInfoEntity>() {
            @Override
            public void onSuccess(MineInfoEntity data) throws Exception {
                mineInfoEntity = data;
                edSchool.setText(mineInfoEntity.getGraduateSchool());
                tvEducation.setText(mineInfoEntity.getEducation());
                edSpecialty.setText(mineInfoEntity.getSpeciality());
                edBornAddress.setText(mineInfoEntity.getRegisteredAddress());
                edAddress.setText(mineInfoEntity.getUsualAddreess());
                edJobAddress.setText(mineInfoEntity.getWorkAddress());
                edPeople.setText(mineInfoEntity.getEmergencyContactName());
                edPhone.setText(mineInfoEntity.getEmergencyContactPhone());
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });

    }

    private void uploadFile() {
        if(mineInfoEntity==null){
            MyToast.show("服务器异常，请稍后重试");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("id", mineInfoEntity.getId());
        map.put("graduateSchool", edSchool.getText().toString());
        map.put("education", tvEducation.getText().toString());
        map.put("speciality", edSpecialty.getText().toString());
        map.put("registeredAddress", edBornAddress.getText().toString());
        map.put("usualAddreess", edAddress.getText().toString());
        map.put("workAddress", edJobAddress.getText().toString());
        map.put("emergencyContactPhone", edPhone.getText().toString());
        map.put("emergencyContactName", edPeople.getText().toString());
        map.put("registeredImage", mineInfoEntity.getRegisteredImage());
        map.put("educationImage", mineInfoEntity.getEducationImage());
        map.put("certificateImage", mineInfoEntity.getCertificateImage());
        map.put("contractImage", mineInfoEntity.getContractImage());
        map.put("contractFile", mineInfoEntity.getContractFile());
        showLoading();
        MyHttpRequest.getDefault().postRequest(Urls.updateEmployeeInfo, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("保存成功");
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    dismissLoading();
                    MyToast.show("提交失败，请稍后重试");
                }

            }
        });
    }
    @OnClick({R.id.common_title_back, R.id.common_right_txt, R.id.tvEducation})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                new InitDialog(UserMoreInfoActivity.this,
                        "提示", "确定要修改么？", "确定", true,
                        new ShareDialogListener() {
                            @Override
                            public void onClick() {
                                uploadFile();
                            }
                        }).show();
                break;
            case R.id.tvEducation:
                new XPopup.Builder(UserMoreInfoActivity.this)
                        .maxWidth(Utils.dip2px(UserMoreInfoActivity.this, 260))
                        .maxHeight(Utils.dip2px(UserMoreInfoActivity.this, 300))
                        .asCenterList("请选择学历", educationArray,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                            tvEducation.setText(text);
                                    }
                                })
                        .show();
                break;
        }
    }
}
